import {getWordSetting} from "@/api/wordSetting";
import {Message} from "element-ui";

export default {
  namespaced: true,
  state() {
    return {
      settingData: {
        objective: 20,
        difficultyLevel: '1',
        dailyReminderTime: null,
        isRemind: false
      }
    }
  },
  actions: {
    getWordSettingAction(context) {
      getWordSetting().then(res => {
        const data = res.data.data;
        //没有添加数据则设置为空对象
        context.commit('getWordSettingMutation',data)
      }).catch(err => {
        Message({
          type: 'error',
          message:'获取设置信息异常'
        })
      })
    }

  },
  mutations: {
    getWordSettingMutation(state,data) {
      if (data && Object.keys(data).length > 0) {
        state.settingData.objective = data.objective
        state.settingData.difficultyLevel = data.difficultyLevel
        state.settingData.isRemind = !!data.dailyReminderTime
        //当前用户设置了提醒时间则渲染
        state.settingData.dailyReminderTime = data.dailyReminderTime
      }
    }
  },
  getters: {
    getWordSetting: state => state.settingData
  }
}
